package com.naiterui.ehp.bs.doctor.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.naiterui.common.repository.page.Page;
import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.ehp.bp.domain.PatientPersonDoctor;
import com.naiterui.ehp.bp.domain.PatientPersonDoctorPK;
import com.naiterui.ehp.bs.doctor.dao.IPatientPersonDoctorDao;
import com.naiterui.ehp.bs.doctor.repository.IPatientPersonDoctorRepository;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 医患关系实体dao
 *
 * @author wangzehao
 */
@Component
public class AdPatientPersonDoctorDaoImpl implements IPatientPersonDoctorDao {

    @Autowired
    private IPatientPersonDoctorRepository patientPersonDoctorRepository;

    @Override
    public PatientPersonDoctor get(PatientPersonDoctorPK doctorPK) {
        return this.patientPersonDoctorRepository.get(doctorPK);
    }

    @Override
    public void update(PatientPersonDoctor doctor) {
        this.patientPersonDoctorRepository.update(doctor);
    }

    @Override
    public List<PatientPersonDoctor> getByPatientIds(Long doctorId, Set<Long> patientId) {

        if (null == patientId || patientId.isEmpty()) {
            return new ArrayList<>();
        }
        List<PatientPersonDoctor> list = this.patientPersonDoctorRepository.getByPatientIds(doctorId, patientId);
        return list;
    }

    @Override
    public List<Long> getPatientIdsByDoctorId(Long doctorId) {
        List<PatientPersonDoctor> list = this.patientPersonDoctorRepository.getPatientIdsByDoctorId(0, doctorId);
        return list.stream().map(PatientPersonDoctor::getPersonPK).map(PatientPersonDoctorPK::getPatientId).collect(Collectors.toList());
    }

    @Override
    public Page<PatientPersonDoctor> getByPeriods(Long doctorId, Date startTime, PageParamVO pageParamVO) {
        Pageable pageable = pageParamVO.getPageable();
        org.springframework.data.domain.Page<PatientPersonDoctor> page = this.patientPersonDoctorRepository.getByPeriods(doctorId, startTime, pageable);
        return Page.buildPage(pageParamVO.getPage(), page.getSize(), page.getContent(), page.getTotalElements());
    }

    @Override
    public List<Long> getDoctorIdsByPatientId(Long patientId) {
        List<PatientPersonDoctor> list = this.patientPersonDoctorRepository.getDoctorIdsByPatientId(patientId);
        return list.stream().map(PatientPersonDoctor::getPersonPK).map(PatientPersonDoctorPK::getDoctorId).collect(Collectors.toList());
    }

    /**
     * 获取医生患者总数
     *
     * @param doctorId
     *
     * @return
     */
    @Override
    public Integer getTotalPatientCount(Long doctorId) {
        return this.patientPersonDoctorRepository.getTotalPatientCount(doctorId);
    }

    /**
     * 初始化医生今日患者数
     *
     * @param doctorId
     * @param date
     *
     * @return
     */
    @Override
    public Long getTodayPatientCount(Long doctorId, Date date) {
        return this.patientPersonDoctorRepository.getTodayPatientCount(doctorId, date);
    }

    @Override
    public int updateRelationFlag(Long docotrId, List<Long> patientIds) {
        Assert.notNull(docotrId, "医生ID不能为null");
        if (CollectionUtil.isEmpty(patientIds)) {
            return 0;
        }

        List<PatientPersonDoctor> list = this.patientPersonDoctorRepository.getByPatientIds(docotrId, patientIds);
        list.forEach(s -> {
            s.setRelationFlag(0);
            s.setOperateDate(new Date());
        });
        this.patientPersonDoctorRepository.update(list);
        return list.size();
    }

    @Override
    public List<Long> getPatientIds(Long doctorId, Integer relationFlag) {
        List<PatientPersonDoctor> patientPersonDoctors = this.patientPersonDoctorRepository.getPatientIdsByDoctorId(relationFlag, doctorId);
        return patientPersonDoctors.stream()
                                   .map(PatientPersonDoctor::getPersonPK)
                                   .map(PatientPersonDoctorPK::getPatientId)
                                   .collect(Collectors.toList());
    }
}
